<template>
	<view>
		<uni-nav-bar left-icon="back" left-text="返回" fixed="true" statusBar="true" @clickLeft="leftAction"
			:right-text="current === 0?'修改':(isMe&&!showUse?'调试':'')" :title="title"
			@clickRight="current === 0?rightAction():(isMe&&!showUse?showDrawer():NULL)"></uni-nav-bar>
		<view class="uni-padding-wrap uni-common-mt">

			<!-- 分段选择器 -->
			<uni-segmented-control :current="current" :values="items" style-type="text" active-color="#4cd964"
				@clickItem="onClickItem"/>
		</view>
		<view class="content">

			<view v-show="current === 0">
				<view>
					<uni-section title="基站信息" type="line"></uni-section>
					<uni-list>
						<template v-for="(item,index) in dataList">
							<uni-list-item :note="item.title" :rightText="item.name" />
						</template>
					</uni-list>
					<uni-section title="调试信息" type="line"></uni-section>
					<uni-list>
						<template v-for="(item,index) in debugList">
							<uni-list-item :note="item.title" :rightText="item.name" />
						</template>
					</uni-list>
					<view class="uni-btn-v">
						<button type="warn" @click="deleteAction">删除</button>
					</view>
				</view>

			</view>
			<view v-show="current === 1">
				<view v-if="showUse">
					<uni-section v-if="isMe" title="您正在使用该基站" type="line"></uni-section>
					<uni-section v-else-if="useList.length>0" :title="'当前基站正在被使用,请先联系'+useList[1].name" type="line">
					</uni-section>
					<uni-section v-else title="error" type="line"></uni-section>

					<view>
						<uni-list>
							<template v-for="(item,index) in useList">
								<uni-list-item :note="item.title" :rightText="item.name" />
							</template>
						</uni-list>
					</view>
					<view v-if='isMe' class="uni-btn-v">
						<button type="primary" @click="goonAction">继续使用</button>
					</view>

					<view v-if='isMe' class="uni-goonbtn-v">
						<button type="warn" @click="logoutAction">退出使用</button>
					</view>
				</view>
				<!-- 调试基站 -->
				<view v-else>

					<view v-if="bsList.length==0" class="uni-goonbtn-v">
						<button type="default" @click="debugAction">获取基站信息</button>
					</view>

					<view v-for="(model,i) in bsList">
						<uni-section :title="model.title" type="line"></uni-section>
						<template v-for="(item,index) in model.list">
							<uni-list-item :note="item.title" :rightText="item.value" />
						</template>
					</view>

					<uni-drawer ref="showRight" mode="right" :mask-click="false">
						<uni-list>
							<template v-for="(item,index) in menuList">
								<uni-list-item :note="item.name" link @click="drawerAction(index)" />
							</template>
						</uni-list>
						<button class="drawer-button" @click="closeDrawer" type="primary">关闭</button>

					</uni-drawer>

				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import uniNavBar from '@/uni_modules/uni-nav-bar/components/uni-nav-bar/uni-nav-bar.vue'
	import uSegmentedControl from '@/uni_modules/uni-segmented-control/components/uni-segmented-control/uni-segmented-control.vue'
	import UniSection from '@/uni_modules/uni-section/components/uni-section/uni-section.vue'
	import uDrawer from '@/uni_modules/uni-drawer/components/uni-drawer/uni-drawer.vue'
	import wsconnect from '@/api/webservice.js'
	export default {
		data() {
			return {
				current: 0,
				title:'',
				bs_id:'',
				items: ['基本信息', '调试'],
				dataList: [],
				debugList: [],
				useList: [],
				showUse: true,
				isMe: false,
				bsList: [],
				menuList: [{
						name: '基站管理',
						path: '../bsManage/bsManage'
					},
					{
						name: '终端管理',
						path: '../clientManage/clientManage'
					},
					{
						name: '测试基站',
						path: '../bsTest/bsTest'
					},
					{
						name: '重启基站',
						action: 'reloadBaseStation',
						path: ''
					}
				],
				useId: ''
			};
		},
		onLoad(options) {
			this.title = options.title
			this.bs_id = options.id
			this.$http.get('/netty/station/' + this.bs_id).then(res => {
				const form = res.data.data

				this.dataList = [{
						"title": "基站名",
						"name": form.name
					},
					{
						"title": "端口号",
						"name": form.port + ''
					},
					{
						"title": "基站位置",
						"name": form.address
					},
					{
						"title": "基站id",
						"name": form.baseStationId
					},
					{
						"title": "备注",
						"name": form.remark
					},

					{
						"title": "基站创建时间",
						"name": form.createTime
					},
					{
						"title": "更新时间",
						"name": form.updateTime
					},
					{
						"title": "最后更新者",
						"name": form.lastUpdateUser
					}

				]
				this.debugList = [{
						"title": "发送点名",
						"name": form.sendCmdTagName
					},
					{
						"title": "接收点名",
						"name": form.reqTagName
					},
					{
						"title": "终端列表点名",
						"name": form.clientListTagName
					}
				]

			}).catch(err => {
				uni.showModal({
					content: '出错了，请稍后再试',
					showCancel: false
				})
			})
			uni.hideLoading()
		},
		components: {
			uniNavBar,
		},
		computed: {
			// 计算属性的 getter
			rightText: function() {
				if (this.current === 0) {
					return '修改';
				} else {
					if (this.isMe && this.showUse) {
						return '调试'
					} else {
						return ''
					}
				}
			}
		},
		methods: {
			onClickItem(e) {
				if (this.current !== e.currentIndex) {
					this.current = e.currentIndex
				}
				if (this.current == 1) {
					this.showUse = true
					this.$http.get("/netty/info/list?isUse=1&bsId=" + this.bs_id).then(res => {
						console.log()
						const results = res.data.rows;
						if (results.length > 0) {
							this.showUse = true
							const form = results[0]
							this.useList = [{
									"title": "基站名",
									"name": form.bsName
								},
								{
									"title": "使用者",
									"name": form.user
								},
								{
									"title": "使用时间",
									"name": form.createTime
								},

							];
							var value = ''
							try {
								value = uni.getStorageSync('login_user');
							} catch {

							}
							if (form.user == value) {
								this.isMe = true
							} else {
								this.isMe = false
							}
							this.useId = form.id
						} else {
							this.showUse = false
							this.isMe = false
							this.useList = []
						}
					}).catch(err => {
						console.log("出错了 err:" + err);
						this.showUse = true
						this.useList = []
						this.isMe = false
					})
				}
			},
			leftAction() {
				uni.navigateBack({
					delta: 1
				});
			},
			rightAction() {
				///修改
				uni.navigateTo({
					url: "../editBasestation/editBasestation?isEdit=1&bs_id=" + this.bs_id,
				})
			},
			deleteAction(e) {
				uni.showModal({
					title: "确定删除？",
					showCancel: true,
					success: (res) => {
						if (res.confirm) {
							this.$http.delete('/netty/station/' + this.bs_id).then(res => {
								if (res.data.code == 200) {
									uni.showModal({
										content: '删除成功！',
										showCancel: false,
										success: (res) => {
											uni.navigateBack({
												delta: 1
											});
										}
									})
								}
							}).catch(err => {
								uni.showModal({
									content: '出错了，请稍后再试',
									showCancel: false
								})
							})


						}

					}
				})
			},
			goonAction() {
				this.showUse = false
				this.useList = []
			},
			logoutAction() { //退出使用
				uni.showModal({
					title: "是否要退出使用",
					showCancel: true,
					success: res => {
						if (res.confirm) {
							this.$http.put('/netty/info/', {
								id: this.useId,
								isUse: 0
							}).then(res => {
								uni.showModal({
									content: '退出成功',
									showCancel: false
								})
								this.showUse = false
								this.isMe = false
								this.useList = []
							}).catch(error => {
								uni.showModal({
									content: '出错了，请稍后再试',
									showCancel: false
								})
							})
						}

					}
				})
			},
			debugAction() {
				///非首次使用
				if (this.isMe) {
					console.log("非首次使用")
				} else { //首次使用，锁死
					var value = ''
					try {
						value = uni.getStorageSync('login_user');
					} catch {}
					var bsName = ''
					if (this.dataList.length > 0) {
						bsName = this.dataList[3].name
					}
					var time = this.$moment().format("yyyy-MM-DD HH:mm:ss");

					this.$http.post('/netty/info/', {
						user: value,
						bsId: this.bs_id,
						isUse: 1,
						bsName: bsName,
						createTime: time
					}).then(res => {
						this.isMe = true
					}).catch(err => {

					})

				}
				if (this.debugList.length == 0) {
					uni.showModal({
						content: '出错了，请稍后再试',
						showCancel: false
					})
					return;
				}
				const sendCmdTagName = this.debugList[0].name
				const reqTagName = this.debugList[1].name

				wsconnect.requestJson({
					items: [sendCmdTagName + '.DESC'],
					vals: ['BsInqRq,//'],
					type: "SetDataByName",
				}, reqTagName).then(res => {
					const arr = res.split('/')
					const array = arr.slice(1, arr.length - 1)
					var dataDic = {}
					for (let i in array) {
						const result = array[i]
						console.log(result)
						const a = result.split('=')
						dataDic[a[0]] = a[1]
					}
					var data_list = []
					var item_list = []
					item_list.push({
						title: "基站时间",
						value: dataDic["T"]
					})
					item_list.push({
						title: "硬件版本",
						value: dataDic["HW"]
					})
					item_list.push({
						title: "软件版本",
						value: dataDic["FW"]
					})
					item_list.push({
						title: "SIM卡",
						value: dataDic["4GSIM"] == 0 ? "关" : "开"
					})
					item_list.push({
						title: "信号质量",
						value: dataDic["4GCSQ"]
					})
					item_list.push({
						title: "配置开关",
						value: dataDic["CFG"] == 1 ? "开" : "关"
					})
					data_list.push({
						title: "基本信息",
						list: item_list
					})
					item_list = []
					item_list.push({
						title: "配置通道",
						value: dataDic["CG"]
					})
					item_list.push({
						title: "数据通道1",
						value: dataDic["D1"]
					})
					item_list.push({
						title: "数据通道2",
						value: dataDic["D2"]
					})
					item_list.push({
						title: "报警通道",
						value: dataDic["AM"]
					})
					data_list.push({
						title: "信道号",
						list: item_list
					})
					item_list = []
					item_list.push({
						title: "IP地址",
						value: dataDic["LI"]
					})
					item_list.push({
						title: "子网掩码",
						value: dataDic["LSUB"]
					})
					item_list.push({
						title: "网关",
						value: dataDic["LGAT"]
					})
					item_list.push({
						title: "端口",
						value: dataDic["LPT"]
					})
					data_list.push({
						title: "基站",
						list: item_list
					})
					item_list = []
					item_list.push({
						title: "IP地址",
						value: dataDic["SI"]
					})
					item_list.push({
						title: "子网掩码",
						value: dataDic["SSUB"]
					})
					item_list.push({
						title: "网关",
						value: dataDic["SGAT"]
					})
					item_list.push({
						title: "端口",
						value: dataDic["SPT"]
					})
					data_list.push({
						title: "服务器",
						list: item_list
					})
					item_list = []
					item_list.push({
						title: "名称",
						value: dataDic["WSID"]
					})
					item_list.push({
						title: "密码",
						value: dataDic["WPW"]
					})
					data_list.push({
						title: "WIFI",
						list: item_list
					})
					item_list = []
					item_list.push({
						title: "数据",
						value: dataDic["DS"]
					})
					item_list.push({
						title: "报警",
						value: dataDic["AS"]
					})
					item_list.push({
						title: "WIFI登录次数",
						value: dataDic["WS"]
					})
					data_list.push({
						title: "离线数据",
						list: item_list
					})
					item_list = []
					item_list.push({
						title: "总数",
						value: dataDic["DT"]
					})
					item_list.push({
						title: "数据通道1",
						value: dataDic["D1T"]
					})
					item_list.push({
						title: "数据通道2",
						value: dataDic["D2T"]
					})
					data_list.push({
						title: "仪表数量",
						list: item_list
					})
					this.bsList = data_list
					wsconnect.clearReqData(1, reqTagName)
				}).catch(error => {
					if (error.code == 1) {
						uni.redirectTo({
							url: "../index/index"
						})
					} else {
						uni.showModal({
							content: error.msg,
							showCancel: false
						})
					}
				})
			},
			showDrawer() {
				this.$refs.showRight.open();
			},
			closeDrawer() {
				this.$refs.showRight.close();
			},
			drawerAction: function(index) {
				const item = this.menuList[index]
				if (item.path.length > 0) {
					uni.navigateTo({
						url: item.path + '?title=' + this.title + '&id=' + this.bs_id
					})
				} else {
					///重启基站
					if (item.action === 'reloadBaseStation') {
						if (this.debugList.length == 0) {
							uni.showModal({
								content: '出错了，请稍后再试',
								showCancel: false
							})
							return;
						}
						const sendCmdTagName = this.debugList[0].name
						const reqTagName = this.debugList[1].name

						uni.showModal({
							showCancel: true,
							title: '是否要重启基站',
							success: res => {
								if (res.confirm) {
									wsconnect.requestJson({
										items: [sendCmdTagName + '.DESC'],
										vals: ['BsRebootRq,//'],
										type: "SetDataByName",
									}, reqTagName).then(res => {
										uni.showToast({
											title: '开始重启，请稍后'
										})
										wsconnect.clearReqData(1, reqTagName)
									}).catch(error => {
										if (error.code == 1) {
											uni.redirectTo({
												url: "../index/index"
											})
										} else {
											uni.showModal({
												content: error.msg,
												showCancel: false
											})
										}
									})
								}
							}
						})
					}
				}
			},

			reloadBaseStation() {

			}
		}

	}
</script>

<style lang="scss">
	@import '@/common/uni-nvue.scss';

	.uni-btn-v {
		margin-left: 50rpx;
		margin-right: 50rpx;
		margin-top: 50rpx;
		height: 40rpx;
	}

	.uni-goonbtn-v {
		margin-left: 50rpx;
		margin-right: 50rpx;
		margin-top: 80rpx;
		height: 40rpx;
	}

	.content {
		// position: fixed;
		// top: 200rpx;
		// width: 100%;
		margin-top: 100rpx;
		
	}
	.drawer-button {
		position: absolute;
		margin-left: 40rpx;
		margin-right: 40rpx;
		width: 80%;
		bottom: 40rpx;
	}

	.uni-padding-wrap {
		// width: 750rpx;
		z-index: 998;
		position: fixed;
		height: 100rpx;
		width: 100%;
		background-color: #FFFFFF;
	}
	.uni-common-mt {
		padding-top: 10px;
		background-color: #FFFFFF;
	}
</style>
